# FastDNS Makefile
# 定义变量
BINARY_NAME_SERVER = fastdns
BINARY_NAME_CLIENT = fastdnsctl
MAIN_SERVER = ./cmd/fastdns/main.go
MAIN_CLIENT = ./cmd/fastdnsctl/main.go
BUILD_DIR = ./build
CONFIG_FILE = ./config.yaml
GO = go
GOFLAGS = -ldflags="-s -w"
VERSION = 1.0.0

# 操作系统和架构
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)

# 颜色输出
GREEN = \033[0;32m
NC = \033[0m # No Color

# 默认目标
.PHONY: all
all: clean build

# 构建目标
.PHONY: build
build: build-server build-client

# 构建服务器
.PHONY: build-server
build-server:
	@echo "${GREEN}构建 FastDNS 服务器...${NC}"
	@mkdir -p $(BUILD_DIR)
	@GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO) build $(GOFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME_SERVER) $(MAIN_SERVER)
	@echo "${GREEN}FastDNS 服务器构建完成: $(BUILD_DIR)/$(BINARY_NAME_SERVER)${NC}"

# 构建客户端
.PHONY: build-client
build-client:
	@echo "${GREEN}构建 FastDNS 客户端...${NC}"
	@mkdir -p $(BUILD_DIR)
	@GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO) build $(GOFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME_CLIENT) $(MAIN_CLIENT)
	@echo "${GREEN}FastDNS 客户端构建完成: $(BUILD_DIR)/$(BINARY_NAME_CLIENT)${NC}"

# 运行服务器
.PHONY: run
run: build-server
	@echo "${GREEN}运行 FastDNS 服务器...${NC}"
	@$(BUILD_DIR)/$(BINARY_NAME_SERVER) -config $(CONFIG_FILE)

# 清理构建产物
.PHONY: clean
clean:
	@echo "${GREEN}清理构建目录...${NC}"
	@rm -rf $(BUILD_DIR)
	@echo "${GREEN}清理完成${NC}"

# 安装到系统
.PHONY: install
install: build
	@echo "${GREEN}安装 FastDNS 到系统...${NC}"
	@cp $(BUILD_DIR)/$(BINARY_NAME_SERVER) /usr/local/bin/
	@cp $(BUILD_DIR)/$(BINARY_NAME_CLIENT) /usr/local/bin/
	@echo "${GREEN}安装完成${NC}"

# 卸载
.PHONY: uninstall
uninstall:
	@echo "${GREEN}从系统卸载 FastDNS...${NC}"
	@rm -f /usr/local/bin/$(BINARY_NAME_SERVER)
	@rm -f /usr/local/bin/$(BINARY_NAME_CLIENT)
	@echo "${GREEN}卸载完成${NC}"

# 测试
.PHONY: test
test:
	@echo "${GREEN}运行测试...${NC}"
	@$(GO) test -v ./...

# 代码格式化
.PHONY: fmt
fmt:
	@echo "${GREEN}格式化代码...${NC}"
	@$(GO) fmt ./...

# 代码检查
.PHONY: lint
lint:
	@echo "${GREEN}检查代码...${NC}"
	@golangci-lint run ./...

# 生成文档
.PHONY: docs
docs:
	@echo "${GREEN}生成文档...${NC}"
	@godoc -http=:6060

# 构建 Docker 镜像
.PHONY: docker
docker: build
	@echo "${GREEN}构建 Docker 镜像...${NC}"
	@docker build -t fastdns:$(VERSION) .

# 发布版本
.PHONY: release
release: clean
	@echo "${GREEN}构建发布版本...${NC}"
	@mkdir -p $(BUILD_DIR)/release
	# Linux amd64
	@GOOS=linux GOARCH=amd64 $(GO) build $(GOFLAGS) -o $(BUILD_DIR)/release/$(BINARY_NAME_SERVER)-linux-amd64 $(MAIN_SERVER)
	@GOOS=linux GOARCH=amd64 $(GO) build $(GOFLAGS) -o $(BUILD_DIR)/release/$(BINARY_NAME_CLIENT)-linux-amd64 $(MAIN_CLIENT)
	# Windows amd64
	@GOOS=windows GOARCH=amd64 $(GO) build $(GOFLAGS) -o $(BUILD_DIR)/release/$(BINARY_NAME_SERVER)-windows-amd64.exe $(MAIN_SERVER)
	@GOOS=windows GOARCH=amd64 $(GO) build $(GOFLAGS) -o $(BUILD_DIR)/release/$(BINARY_NAME_CLIENT)-windows-amd64.exe $(MAIN_CLIENT)
	# macOS amd64
	@GOOS=darwin GOARCH=amd64 $(GO) build $(GOFLAGS) -o $(BUILD_DIR)/release/$(BINARY_NAME_SERVER)-darwin-amd64 $(MAIN_SERVER)
	@GOOS=darwin GOARCH=amd64 $(GO) build $(GOFLAGS) -o $(BUILD_DIR)/release/$(BINARY_NAME_CLIENT)-darwin-amd64 $(MAIN_CLIENT)
	# macOS arm64
	@GOOS=darwin GOARCH=arm64 $(GO) build $(GOFLAGS) -o $(BUILD_DIR)/release/$(BINARY_NAME_SERVER)-darwin-arm64 $(MAIN_SERVER)
	@GOOS=darwin GOARCH=arm64 $(GO) build $(GOFLAGS) -o $(BUILD_DIR)/release/$(BINARY_NAME_CLIENT)-darwin-arm64 $(MAIN_CLIENT)
	@echo "${GREEN}发布版本构建完成: $(BUILD_DIR)/release/${NC}"

# 帮助信息
.PHONY: help
help:
	@echo "FastDNS Makefile 帮助"
	@echo ""
	@echo "可用命令:"
	@echo "  make              - 清理并构建项目"
	@echo "  make build        - 构建服务器和客户端"
	@echo "  make build-server - 仅构建服务器"
	@echo "  make build-client - 仅构建客户端"
	@echo "  make run          - 构建并运行服务器"
	@echo "  make clean        - 清理构建目录"
	@echo "  make install      - 安装到系统"
	@echo "  make uninstall    - 从系统卸载"
	@echo "  make test         - 运行测试"
	@echo "  make fmt          - 格式化代码"
	@echo "  make lint         - 检查代码"
	@echo "  make docs         - 生成文档"
	@echo "  make docker       - 构建 Docker 镜像"
	@echo "  make release      - 构建多平台发布版本"
	@echo "  make help         - 显示帮助信息"
	@echo ""
	@echo "环境变量:"
	@echo "  GOOS              - 目标操作系统 (默认: $(GOOS))"
	@echo "  GOARCH            - 目标架构 (默认: $(GOARCH))"
